/**
 * 
 */
package de.cbf.cam.dsa.facet.talent.edit;

import static de.cbf.util.ui.test_support.ConstantsSWTBOTUtil.DEFAULT_SWT_BOT_KEY;

import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

import de.cbf.cam.dsa.facet.talent.helper.BeTypeValue;
import de.cbf.cam.dsa.facet.talent.helper.CompensationTalentReference;
import de.cbf.cam.dsa.facet.talent.test_support.ConstantsSWTBOTTalent;
import de.cbf.util.entity.facet.edit.composites.AbstractCompositeEditFacet;
import de.cbf.util.entity.facet.edit.util.FieldDecoratorUtil;
import de.cbf.util.ui.composites.CompositeListEdit;
import de.cbf.util.ui.composites.CompositeTableEdit;
import de.cbf.util.ui.listener.UiChangeEvent;

/**
 * @author Nebelritter
 *
 */
public class CompositeEditTalent extends AbstractCompositeEditFacet {
	/** min: , default: ,max:  */
	public static final int HEIGHT_HINT_BE_SOURCES				= 100;
	/** min: , default: ,max:  */
	public static final int HEIGHT_HINT_SPECIALIZATIONS			=  -1;
	/** min: , default: ,max:  */
	public static final int HEIGHT_HINT_COMPENSATION_TALENTS	=  -1;
	
	public static final int EVT_EDIT_TALENT_TYPE			= 10;
	public static final int EVT_EDIT_ATTRIBUTE_FIRST		= 11;
	public static final int EVT_EDIT_ATTRIBUTE_SECOND		= 12;
	public static final int EVT_EDIT_ATTRIBUTE_THIRD		= 13;
	public static final int EVT_EDIT_RAISE_COLUMN			= 14;
	public static final int EVT_EDIT_EBE					= 15;
	public static final int EVT_EDIT_PURE_AT_COMBAT_TALENT	= 16;
	public static final int EVT_EDIT_SPECIALIZATIONS		= 17;
	public static final int EVT_EDIT_COMPENSATION_TALENTS	= 18;

	public static final String TEXT_LABEL_TALENT_TYPE			= "Talent type: ";
	public static final String TEXT_LABEL_ATTRIBUTE_FIRST		= "Attribute(first): ";
	public static final String TEXT_LABEL_ATTRIBUTE_SECOND		= "Attribute(second): ";
	public static final String TEXT_LABEL_ATTRIBUTE_THIRD		= "Attribute(third): ";
	public static final String TEXT_LABEL_RAISE_COLUMN			= "Raise Column: ";	
	public static final String TEXT_LABEL_PURE_AT_COMBAT_TALENT	= "Pure AT Combat Talent";
	public static final String TEXT_BE_SOURCES					= "BE Sources.. ";
	public static final String TEXT_SPECIALIZATIONS				= "Specializations..";
	public static final String TEXT_COMPENSATION_TALENTS		= "Compensation Talents..";
			
	protected Combo cb_TalentType;
	protected Combo cb_AttributeFirst;
	protected Combo cb_AttributeSecond;
	protected Combo cb_AttributeThird;
	protected Text tx_RaiseColumn;
	protected ControlDecoration decoration_RaiseColumn;
	protected Button bt_PureATCombatTalent;
	protected CompositeTableEdit<BeTypeValue> cp_BeTypeValues;
	protected CompositeListEdit cp_Specializations;	
	protected CompositeTableEdit<CompensationTalentReference> cp_CompensationTalents;	
	
	public CompositeEditTalent(Composite parent, int style, FormToolkit toolkit) {
		super(parent, style, toolkit);		
	}

	@Override
	protected GridLayout createLayout() {
		return new GridLayout(2, false);
	}
		
	@Override
	protected void createContent(Composite compositeThis) {
		
		//fTalentType
		Label lb_TalentType = toolkit.createLabel(compositeThis, TEXT_LABEL_TALENT_TYPE,SWT.NONE);
		lb_TalentType.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));

		cb_TalentType = new Combo(compositeThis,SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
		toolkit.adapt(cb_TalentType);
		cb_TalentType.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		
		cb_TalentType.setData(DEFAULT_SWT_BOT_KEY,ConstantsSWTBOTTalent.FACET_Talent_COMBO_TALENT_TYPE_ID);		
		cb_TalentType.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditTalent.this);
				event.setNewValue(cb_TalentType.getText());
				event.setIndex(EVT_EDIT_TALENT_TYPE);
				fireUIChangeEvent(event);
			}
		});	
		
		//fAttributeFirst
		Label lb_AttributeFirst = toolkit.createLabel(compositeThis, TEXT_LABEL_ATTRIBUTE_FIRST,SWT.NONE);
		lb_AttributeFirst.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));

		cb_AttributeFirst = new Combo(compositeThis,SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
		toolkit.adapt(cb_AttributeFirst);
		cb_AttributeFirst.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		cb_AttributeFirst.setData(DEFAULT_SWT_BOT_KEY,ConstantsSWTBOTTalent.FACET_Talent_COMBO_ATTRIBUTE_FIRST_ID);
		cb_AttributeFirst.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditTalent.this);
				event.setNewValue(cb_AttributeFirst.getText());
				event.setIndex(EVT_EDIT_ATTRIBUTE_FIRST);
				fireUIChangeEvent(event);
			}
		});	
		
		//fAttributeSecond
		Label lb_AttributeSecond = toolkit.createLabel(compositeThis, TEXT_LABEL_ATTRIBUTE_SECOND,SWT.NONE);
		lb_AttributeSecond.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));

		cb_AttributeSecond = new Combo(compositeThis,SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
		toolkit.adapt(cb_AttributeSecond);
		cb_AttributeSecond.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		cb_AttributeSecond.setData(DEFAULT_SWT_BOT_KEY,ConstantsSWTBOTTalent.FACET_Talent_COMBO_ATTRIBUTE_SECOND_ID);
		cb_AttributeSecond.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditTalent.this);
				event.setNewValue(cb_AttributeSecond.getText());
				event.setIndex(EVT_EDIT_ATTRIBUTE_SECOND);
				fireUIChangeEvent(event);			
			}
		});	
		
		//fAttributeThird
		Label lb_AttributeThird = toolkit.createLabel(compositeThis, TEXT_LABEL_ATTRIBUTE_THIRD,SWT.NONE);
		lb_AttributeThird.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));

		cb_AttributeThird = new Combo(compositeThis,SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
		toolkit.adapt(cb_AttributeThird);
		cb_AttributeThird.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		cb_AttributeThird.setData(DEFAULT_SWT_BOT_KEY,ConstantsSWTBOTTalent.FACET_Talent_COMBO_ATTRIBUTE_THIRD_ID);
		cb_AttributeThird.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditTalent.this);
				event.setNewValue(cb_AttributeThird.getText());
				event.setIndex(EVT_EDIT_ATTRIBUTE_THIRD);
				fireUIChangeEvent(event);
			}
		});	
			
		//fRaiseColumn
		Label lb_RaiseColumn = toolkit.createLabel(compositeThis, TEXT_LABEL_RAISE_COLUMN,SWT.NONE);
		lb_RaiseColumn.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));

		tx_RaiseColumn = toolkit.createText(compositeThis, "",SWT.BORDER);
		tx_RaiseColumn.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		tx_RaiseColumn.setData(DEFAULT_SWT_BOT_KEY,ConstantsSWTBOTTalent.FACET_Talent_TEXT_RAISE_COLUMN_ID);
		tx_RaiseColumn.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditTalent.this);
				event.setNewValue(tx_RaiseColumn.getText());
				event.setIndex(EVT_EDIT_RAISE_COLUMN);
				fireUIChangeEvent(event);
			}
		});			
		// Create the decoration for fRaiseColumn
		decoration_RaiseColumn = new ControlDecoration(tx_RaiseColumn,SWT.LEFT|SWT.TOP);
		// Re-use an existing image
		Image image_RaiseColumn = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_ERROR).
		  getImage();
		// Set description and image
		decoration_RaiseColumn.setDescriptionText(FieldDecoratorUtil.TOOLTIP_TEXT_ENTER_NUMBERS_ANY);
		decoration_RaiseColumn.setImage(image_RaiseColumn);
		decoration_RaiseColumn.hide();
		
		//pureATCombatTalent
		bt_PureATCombatTalent = toolkit.createButton(compositeThis,TEXT_LABEL_PURE_AT_COMBAT_TALENT,SWT.CHECK);		
		bt_PureATCombatTalent.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,1));
		bt_PureATCombatTalent.setData(DEFAULT_SWT_BOT_KEY,ConstantsSWTBOTTalent.FACET_Talent_BUTTON_PURE_AT_COMBAT_TALENT_ID);
		bt_PureATCombatTalent.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditTalent.this);
				event.setNewValue(bt_PureATCombatTalent.getSelection());
				event.setIndex(EVT_EDIT_PURE_AT_COMBAT_TALENT);
				fireUIChangeEvent(event);
			}
		});		
		
		//beSources
		cp_BeTypeValues = createTableComposite_BESources(TEXT_BE_SOURCES, compositeThis);
		GridData gd_BeTypeValues = new GridData(SWT.FILL,SWT.FILL,true,true,2,1);
		gd_BeTypeValues.heightHint = HEIGHT_HINT_BE_SOURCES;
		cp_BeTypeValues.setLayoutData(gd_BeTypeValues);
		cp_BeTypeValues.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTTalent.FACET_Talent_COMPOSITE_BE_SOURCES_EDIT_ID);
					
		//specializations
		cp_Specializations = createListComposite(TEXT_SPECIALIZATIONS, compositeThis);
		GridData gd_Specializations = new GridData(SWT.FILL,SWT.FILL,true,true,2,1);
		gd_Specializations.heightHint = HEIGHT_HINT_SPECIALIZATIONS;
		cp_Specializations.setLayoutData(gd_Specializations);
		cp_Specializations.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTTalent.FACET_Talent_COMPOSITE_SPECIALIZATIONS_EDIT_ID);
		
		//compensationTalents
		cp_CompensationTalents = createTableComposite_CompensationTalents(TEXT_COMPENSATION_TALENTS, compositeThis);
		GridData gd_CompensationTalents = new GridData(SWT.FILL,SWT.FILL,true,true,2,1);
		gd_CompensationTalents.heightHint = HEIGHT_HINT_COMPENSATION_TALENTS;
		cp_CompensationTalents.setLayoutData(gd_CompensationTalents);
		cp_CompensationTalents.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTTalent.FACET_Talent_COMPOSITE_COMPENSATION_TALENTS_EDIT_ID);
	}
	
	
	/**
	 * @param labelText
	 * @param parent
	 */
	protected CompositeListEdit createListComposite(String labelText,Composite parent) {
		CompositeListEdit cp_ListEdit = new CompositeListEdit(parent, SWT.DOUBLE_BUFFERED);
		cp_ListEdit.setHeaderText(labelText);
		return cp_ListEdit;
	}
	
	protected CompositeTableEdit<CompensationTalentReference> createTableComposite_CompensationTalents(
			String textCompensationTalents, Composite parent) {
		CompositeTableEdit<CompensationTalentReference> composite =
				CompositeTableEdit.createComposite(CompensationTalentReference.class, parent, SWT.DOUBLE_BUFFERED);
		composite.setHeaderText(textCompensationTalents);
		return composite;
	}

	protected CompositeTableEdit<BeTypeValue> createTableComposite_BESources(
			String textBeSource, Composite parent) {
		CompositeTableEdit<BeTypeValue> composite =
				CompositeTableEdit.createComposite(BeTypeValue.class, parent, SWT.DOUBLE_BUFFERED);
		composite.setHeaderText(textBeSource);		
		return composite;
	}	
	
	public CompositeTableEdit<BeTypeValue> getCompositeBeSources(){
		return cp_BeTypeValues;				
	}
	
	public CompositeListEdit getCompositeSpecializations(){
		return cp_Specializations;				
	}
	
	public CompositeTableEdit<CompensationTalentReference> getCompositeCompensationTalents(){
		return cp_CompensationTalents;				
	}
	
	public void setPossibleAttributesFirst(String[] attributes){
		String selected = cb_AttributeFirst.getText();
		cb_AttributeFirst.setItems(attributes);
		int selectionIndex = cb_AttributeFirst.indexOf(selected);
		cb_AttributeFirst.select(selectionIndex);
	}
	
	public void setPossibleAttributesSecond(String[] attributes){
		String selected = cb_AttributeSecond.getText();
		cb_AttributeSecond.setItems(attributes);
		int selectionIndex = cb_AttributeSecond.indexOf(selected);
		cb_AttributeSecond.select(selectionIndex);
	}
	
	public void setPossibleAttributesThird(String[] attributes){
		String selected = cb_AttributeThird.getText();
		cb_AttributeThird.setItems(attributes);
		int selectionIndex = cb_AttributeThird.indexOf(selected);
		cb_AttributeThird.select(selectionIndex);
	}
	
	public void setPossibleTalentTypes(String[] talentTypes){
		String selected = cb_TalentType.getText();
		cb_TalentType.setItems(talentTypes);
		int selectionIndex = cb_TalentType.indexOf(selected);
		cb_TalentType.select(selectionIndex);
	}
	
	public String getTalentType(){
		return cb_TalentType.getText();
	}
	
	public void setTalentType(String value) {
		if(value == null){
			cb_TalentType.deselectAll();
			return;
		}
		if( ! cb_TalentType.getText().equals(value)){
			cb_TalentType.setText(value);
		}
	}
	
	public String getAttributeFirst(){
		return cb_AttributeFirst.getText();
	}	
	public void setAttributeFirst(String value) {
		if(value == null){
			cb_AttributeFirst.deselectAll();
			return;
		}
		if( ! cb_AttributeFirst.getText().equals(value)){			
			int index = cb_AttributeFirst.indexOf(value);
			cb_AttributeFirst.select(index);
		}
	}
	
	public String getAttributeSecond(){
		return cb_AttributeSecond.getText();
	}
	public void setAttributeSecond(String value) {
		if(value == null){
			cb_AttributeSecond.deselectAll();
			return;
		}
		if( ! cb_AttributeSecond.getText().equals(value)){			
			int index = cb_AttributeSecond.indexOf(value);
			cb_AttributeSecond.select(index);
		}
	}
	
	public String getAttributeThird(){
		return cb_AttributeThird.getText();
	}
	public void setAttributeThird(String value) {
		if(value == null){
			cb_AttributeThird.deselectAll();
			return;
		}
		if( ! cb_AttributeThird.getText().equals(value)){			
			int index = cb_AttributeThird.indexOf(value);
			cb_AttributeThird.select(index);
		}
	}
		
	public String getRaiseColumn(){
		return tx_RaiseColumn.getText();
	}	
	public void setRaiseColumn(String value) {
		if( ! tx_RaiseColumn.getText().equals(value)){
			tx_RaiseColumn.setText(value);
		}
	}	
	public void setDecorationRaiseColumnStatus(boolean active) {
		if(active){
			decoration_RaiseColumn.show();
			tx_RaiseColumn.setBackground(cl_Text_red);
		}else{
			decoration_RaiseColumn.hide();
			tx_RaiseColumn.setBackground(cl_Text_list_bg);
		}
	}
	
	public Boolean getPureATCombatTalent(){
		return bt_PureATCombatTalent.getSelection();
	}

	public void setPureATCombatTalent(Boolean value) {
		if( ! bt_PureATCombatTalent.getSelection() == value){
			bt_PureATCombatTalent.setSelection(value);
		}
	}
}
